package top.drinyeit.weilong.adapter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import top.drinyeit.weilong.R;
import top.drinyeit.weilong.model.SecCategoryBean;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/3/20.
 */

public class ChildAdapter extends RecyclerView.Adapter<ChildAdapter.ViewHolder> {

    private Context context;
    private List<SecCategoryBean> list;
    private Map<Integer, Boolean> map = new HashMap<>();
    private boolean onBind;
    private Handler handler;
    private int groupIndex;

    public ChildAdapter(List<SecCategoryBean> list, Handler handler, int groupIndex) {
        this.list = list;
        this.handler = handler;
        this.groupIndex = groupIndex;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        context = parent.getContext();
        View view = LayoutInflater.from(context).inflate(R.layout.item_child, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        holder.checkBox.setText(list.get(position).getCategory_name());
        holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    map.clear();
                    map.put(position, true);
                }

                if (!onBind) {
                    notifyDataSetChanged();
                    System.out.println("position" + position);
                    Message message = Message.obtain(handler, 1, groupIndex,position);
                    message.sendToTarget();
                }
            }
        });
        onBind = true;
        if (map != null && map.containsKey(position)) {
            holder.checkBox.setChecked(true);
            holder.checkBox.setBackgroundColor(ContextCompat.getColor(context, R.color.text_line_220));
            holder.checkBox.setTextColor(ContextCompat.getColor(context,R.color.text_black_51));

        } else {
            holder.checkBox.setChecked(false);
            holder.checkBox.setBackgroundColor(ContextCompat.getColor(context, R.color.white));
            holder.checkBox.setTextColor(ContextCompat.getColor(context,R.color.hint_153));
        }
        onBind = false;
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        public CheckBox checkBox;

        public ViewHolder(View itemView) {
            super(itemView);
            checkBox = itemView.findViewById(R.id.cb_child);
        }

    }
}
